<?php
/**
 * 服务模块
 *
 * User: Jay
 * Date: 2018/8/8
 */

namespace AdminBundle\Controller;


use AdminBundle\RBAC\Item;
use AdminBundle\RBAC\Role;
use AdminBundle\Entity\AuthItem;
use AdminBundle\Model\m_admin;
use Symfony\Component\HttpFoundation\Request;

class AuthRoleController extends AdminController
{
    public function listAction()
    {
        $r = $this->inlet($this->getParameter('return_page'), true);
        if ($r !== true) {
            return $r;
        }

        $roles = $this->getDoctrine()
            ->getRepository(AuthItem::class)
            ->findBy(array('type' => Item::TYPE_ROLE));

        return $this->render('@Admin/admin/Authorization/role-list.html.twig', array(
                'info' => $roles
            )
        );
    }

    public function showAction($name)
    {
        $r = $this->inlet($this->getParameter('return_page'), true);
        if ($r !== true) {
            return $r;
        }

        if ($name) {
            $role = $this->get('rbac')->getRole($name);

            if (!$role) {
                $this->redirectToRoute('admin_role_list');
            }
        } else {
            $role = new Role();
        }

        return $this->render('@Admin/admin/Authorization/role-form.html.twig', array(
                'role' => $role,
                'name' => $name
            )
        );
    }

    public function addAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), true);
        if ($r !== true) {
            return $r;
        }

        $rbac = $this->get('rbac');

        $role = $rbac->createRole(self::$request->request->get('name'));
        $role->description = self::$request->request->get('description');

        try {
            $rbac->add($role);

            return $this->response('操作成功', 0);
        } catch (\Exception $exception) {
            return $this->response($exception->getMessage(), 1);
        }
    }

    public function updateAction($name)
    {
        $r = $this->inlet($this->getParameter('return_data'), true);
        if ($r !== true) {
            return $r;
        }

        $item = new Item();
        $item->name = self::$request->request->get('name');
        $item->description = self::$request->request->get('description');

        $error = '';
        try {
            if ($this->get('rbac')->update($name, $item)) {
                return $this->response('操作成功', 0);
            } else {
                $error = '数据更新失败';
            }
        } catch (\Exception $exception) {
            $error = $exception->getMessage();
        }

        return $this->response($error, 1);
    }

    public function deleteAction($name)
    {
        $r = $this->inlet($this->getParameter('return_data'), true);
        if ($r !== true) {
            return $r;
        }

        $rbac = $this->get('rbac');
        $role = $rbac->getRole($name);
        if ($role) {
            $rbac->remove($role);
        }

        return $this->response('操作成功', 0);
    }

    public function childrenAction($name)
    {
        $r = $this->inlet($this->getParameter('return_page'), true);
        if ($r !== true) {
            return $r;
        }

        $item = $this->getDoctrine()->getRepository(AuthItem::class)->find($name);

        if (!$item) {
            return $this->redirectToRoute('admin_role_list');
        }

        return $this->render('@Admin/admin/Authorization/role-children.html.twig', array(
                'item' => $item
            )
        );
    }

    public function assignAction(Request $request)
    {
        $r = $this->inlet($this->getParameter('return_data'), true);
        if ($r !== true) {
            return $r;
        }

        $name = $request->request->get('id');
        $action = $request->request->get('action');
        $roles = $request->request->get('roles');

        $rbac = $this->get('rbac');

        $parent = $rbac->getItem($name);

        if (!$parent) {
            return $this->response('角色不存在', 1);
        }

        try {
            foreach ($roles as $child) {
                if ($action == 'assign') {
                    $rbac->addChild($parent, $rbac->getItem($child));
                } else {
                    $rbac->removeChild($name, $child);
                }
            }
        } catch (\Exception $exception) {
            return $this->response($exception->getMessage(), 1);
        }

        return $this->response('操作成功', 0);
    }

    /**
     * 添加与编辑页面
     *
     * @return bool|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function infoAction(){
        $r = $this->inlet('data');
        if ($r !== true) {
            return $r;
        }

        $m_admin = new m_admin($this->container);
        $id = self::$request->query->get('id');

        $data['info'] = [];
        if (!empty($id)) {
            $data = $m_admin->getInfo($id);
        }

        return $this->render('@Admin/admin/Authorization/role-info.html.twig', $data);
    }

    /**
     * 添加与编辑操作
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function insertOrUpdateAction(){
        $r = $this->inlet('data');
        if ($r !== true) {
            return $r;
        }

        $m_admin = new m_admin($this->container);

        $id = self::$request->request->get('id');
        $data['account'] = self::$request->request->get('account');
        $data['name'] = self::$request->request->get('name');
        $password = self::$request->request->get('password');
        $ensure_password = self::$request->request->get('ensure_password');
        $data['password'] = $password;

        if(empty($data['account'])){
            return $this->response('请填写登陆账号',1);
        }

        if (empty($data['name'])){
            return $this->response('请填写管理员名称',1);
        }

        if (empty($data['password'])){
            return $this->response('请填写密码',1);
        }

        //数据验证
        if ($password!= $ensure_password){
            return $this->response('新密码两次输入不一致');
        }

        if (empty($id)){
            if($m_admin->insert($data)) {
                return $this->response('添加帐号成功', 0);
            }
        }else{
            if ($m_admin->update($id,$data)){
                return $this->response('编辑帐号成功', 0);
            }

        }

        return $this->response(self::$error->getError());
    }

    /**
     * 删除操作
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function accountdeleteAction()
    {
        $r = $this->inlet('data');
        if ($r !== true) {
            return $r;
        }
        $m_admin = new m_admin($this->container);
        $id = self::$request->query->get('id');
        $data['is_del']=1;
        $re = $m_admin->update($id,$data);
        return $this->response('删除成功', 0);
    }

}